/*
 * COPYRIGHT:	2010 Christopher Smith, licenced under the GNU GPL v2; see COPYING in the top level directory
 * PROJECT:		sdedit
 * FILE:		opts gui.cpp
 * PURPOSE:		GUI user options
 *				especially for the ACL editor
 */

namespace GuiOpt
{

SDE_OPT Options[] =
{
	{L"FontName", SDE_OPT_SET, SptString, (PVOID)L"MS Shell Dlg"},
	{L"FontSize", 0, SptInt32},
	{L"BitColumnWidth", 0, SptInt32},
	{L"PadWitdh", 0, SptInt32},
	{L"TopLabelHeight", 0, SptInt32},
	{L"MenuBreakFactor", SDE_OPT_SET, SptInt32, (PVOID)40},
	{L"DefaultAclMergeMode", SDE_OPT_SET, SptInt32, (PVOID)3},
	{L"DefaultMergeAll", SDE_OPT_SET, SptBool, (PVOID)FALSE},
	{L"WindowsIndependent", SDE_OPT_SET, SptBool, (PVOID)FALSE},
	{L"LargeIcons", 0, SptBool},
	{L"ShowSid", SDE_OPT_SET, SptBool, (PVOID)FALSE},
	{L"ShowSpecific", SDE_OPT_SET, SptBool, (PVOID)TRUE},
	{L"ShowStandard", SDE_OPT_SET, SptBool, (PVOID)TRUE},
	{L"ShowGeneric", SDE_OPT_SET, SptBool, (PVOID)TRUE},
	{L"ShowExtra", SDE_OPT_SET, SptBool, (PVOID)FALSE},
	{L"ShowObject", SDE_OPT_SET, SptBool, (PVOID)TRUE},
	{L"ShowObjectInherit", SDE_OPT_SET, SptBool, (PVOID)TRUE},
	{L"ShowInheritSource", SDE_OPT_SET, SptBool, (PVOID)TRUE}
};

struct SDE_GUI_PREFS //order synched with Options
{
	HFONT Font;
	int FontSize;
	int BitColumnWidth;
	int PadWidth;
	int TopLabelHeight;
	USHORT MenuBreakFactor;
	UCHAR DefaultAclMergeMode;
	BOOLEAN DefaultMergeAll;
	BOOLEAN WindowsIndependent;
	BOOLEAN LargeIcons;
	BOOLEAN ShowSid;
	BOOLEAN ShowSpecific;
	BOOLEAN ShowStandard;
	BOOLEAN ShowGeneric;
	BOOLEAN ShowExtra;
	BOOLEAN ShowObject;
	BOOLEAN ShowObjectInherit;
	BOOLEAN ShowInhSource;
};

SDE_OPTS Opts =
{
	RTL_NUMBER_OF(Options),
	Options
};

struct GuiPrefsTranslated
{
	SDE_GUI_PREFS Prefs;

	GuiPrefsTranslated(SDE_OPTS *GuiOptions)
	{
		RtlZeroStruct(Prefs);

		SDE_OPT *Opts = GuiOptions->Opts;

		HDC Desktop = RaiseOnWin32ErrorT(GetDC(NULL));

		__try
		{
			double ScaleX = GetDeviceCaps(Desktop, LOGPIXELSX) / 96.0;
			double ScaleY = GetDeviceCaps(Desktop, LOGPIXELSY) / 96.0;

			if(Opts[1].Flags & SDE_OPT_SET) //calculate if not set
				Prefs.FontSize = Opts[1].Int32;
			else
				Prefs.FontSize = static_cast<int>(14 * ScaleX);

			Prefs.Font = CreateFont(Prefs.FontSize, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_SWISS, Opts[0].String);
			if(Opts[2].Flags & SDE_OPT_SET)
				Prefs.BitColumnWidth = Opts[2].Int32;
			else
				Prefs.BitColumnWidth = static_cast<int>(Prefs.FontSize * 1.1);

			if(Opts[3].Flags & SDE_OPT_SET)
				Prefs.PadWidth = Opts[3].Int32;
			else
				Prefs.PadWidth = static_cast<int>(5 * ScaleX);

			if(Opts[4].Flags & SDE_OPT_SET)
				Prefs.TopLabelHeight = Opts[4].Int32;
			else
				Prefs.TopLabelHeight = static_cast<int>(100 * ScaleY);

			Prefs.MenuBreakFactor = c_cast<USHORT>(Opts[5].Int32 & MAXUSHORT);
			if(Opts[6].Int32 > (ACL_MERGE_MODE_MAX >> ACL_MERGE_MODE_SHIFT))
				RaiseTextError(L"Invalid default ACL merge mode");
			Prefs.DefaultAclMergeMode = c_cast<UCHAR>(Opts[6].Int32);
			Prefs.DefaultMergeAll = Opts[7].Bool != 0;
			Prefs.WindowsIndependent = Opts[8].Bool != 0;

			if(Opts[9].Flags & SDE_OPT_SET)
				Prefs.LargeIcons = Opts[9].Bool != 0;
			else
				Prefs.LargeIcons = ScaleX >= 1.5 ? TRUE : FALSE;

			Prefs.ShowSid = Opts[10].Bool != 0;
			Prefs.ShowSpecific = Opts[11].Bool != 0;
			Prefs.ShowStandard = Opts[12].Bool != 0;
			Prefs.ShowGeneric = Opts[13].Bool != 0;
			Prefs.ShowExtra = Opts[14].Bool != 0;
			Prefs.ShowObject = Opts[15].Bool != 0;
			Prefs.ShowObjectInherit = Opts[16].Bool != 0;
			Prefs.ShowInhSource = Opts[17].Bool != 0;
		}
		__finally
		{
			ReleaseDC(NULL, Desktop);
		}
	}

	~GuiPrefsTranslated()
	{
		DeleteObject(Prefs.Font);
	}
};

}//namespace GuiOpt